home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / swish.11 / src / hash.c < prev    next >
C/C++ Source or Header  |  1995-03-11  |  5KB  |  224 lines

  1. /*
  2. ** Copyright (C) 1995, Enterprise Integration Technologies Corp.        
  3. ** All Rights Reserved.
  4. ** Kevin Hughes, kevinh@eit.com 
  5. ** 3/11/94
  6. */
  7.  
  8. #include "swish.h"
  9. #include "hash.h"
  10.  
  11. /* Hashes a string.
  12. */
  13.  
  14. unsigned hash(s)
  15.      char *s;
  16. {
  17.         unsigned hashval;
  18.  
  19.         for (hashval = 0; *s != '\0'; s++)
  20.                 hashval = *s + 31 * hashval;
  21.         return hashval % HASHSIZE;
  22. }
  23.  
  24. /* Hashes a string for a larger hash table.
  25. */
  26.  
  27. unsigned bighash(s)
  28.      char *s;
  29. {
  30.         unsigned hashval;
  31.  
  32.         for (hashval = 0; *s != '\0'; s++)
  33.                 hashval = *s + 31 * hashval;
  34.         return hashval % BIGHASHSIZE;
  35. }
  36.  
  37. /* Reads the internal list of default stopwords.
  38. */
  39.  
  40. void readdefaultstopwords()
  41. {
  42.         int i;
  43.  
  44.         for (i = 0; defaultstopwords[i] != NULL; i++)
  45.                 addstophash(defaultstopwords[i]);
  46. }
  47.  
  48. /* Adds a stop word to a hash table.
  49. */
  50.  
  51. void addstophash(word)
  52.      char *word;
  53. {
  54.         unsigned hashval;
  55.         struct swline *sp;
  56.  
  57.     if (isstopword(word))
  58.         return;
  59.  
  60.         sp = (struct swline *) emalloc(sizeof(struct swline));
  61.         sp->line = (char *) mystrdup(word);
  62.  
  63.         hashval = hash(word);
  64.         sp->next = hashstoplist[hashval];
  65.         hashstoplist[hashval] = sp;
  66. }
  67.  
  68. /* Sees if a word is a stop word by looking it up in the hash table.
  69. */
  70.  
  71. int isstopword(word)
  72.      char *word;
  73. {
  74.         unsigned hashval;
  75.         struct swline *sp;
  76.  
  77.         hashval = hash(word);
  78.         sp = hashstoplist[hashval];
  79.  
  80.         while (sp != NULL) {
  81.                 if (!strcmp(sp->line, word))
  82.                         return 1;
  83.                 sp = sp->next;
  84.         }
  85.         return 0;
  86. }
  87.  
  88. /* Adds a file number and its associated file location
  89. ** to a hash table.
  90. */
  91.  
  92. void addtofilehashlist(fileshort, filelong)
  93.      int fileshort;
  94.      long filelong;
  95. {
  96.         unsigned hashval;
  97.         struct filenum *fp;
  98.         char tmpstr[MAXSTRLEN];
  99.  
  100.         fp = (struct filenum *) emalloc(sizeof(struct filenum));
  101.         fp->fileshort = fileshort;
  102.         fp->filelong = filelong;
  103.  
  104.         sprintf(tmpstr, "%d", fileshort);
  105.         hashval = bighash(tmpstr);
  106.         fp->next = filehashlist[hashval];
  107.         filehashlist[hashval] = fp;
  108. }
  109.  
  110. /* Looks up a file number in the hash table and
  111. ** returns the file position of the associated file info.
  112. */
  113.  
  114. long getfilenum(filenum)
  115.      int filenum;
  116. {
  117.         unsigned hashval;
  118.         struct filenum *fp;
  119.         char tmpstr[MAXSTRLEN];
  120.  
  121.         sprintf(tmpstr, "%d", filenum);
  122.         hashval = bighash(tmpstr);
  123.         fp = filehashlist[hashval];
  124.  
  125.         while (fp != NULL) {
  126.                 if (fp->fileshort == filenum)
  127.                         return fp->filelong;
  128.                 fp = fp->next;
  129.         }
  130.         return 0;
  131. }
  132.  
  133. /* Adds a file number and the number of indexed words
  134. ** to a hash table.
  135. */
  136.  
  137. void addtofwordtotals(filenum, ftotalwords)
  138.      int filenum;
  139.      int ftotalwords;
  140. {
  141.         unsigned hashval;
  142.         struct fwordtotal *fp;
  143.         char tmpstr[MAXSTRLEN];
  144.  
  145.         fp = (struct fwordtotal *) emalloc(sizeof(struct fwordtotal));
  146.         fp->filenum = filenum;
  147.         fp->totalwords = ftotalwords;
  148.         fp->next = NULL;
  149.  
  150.         sprintf(tmpstr, "%d", filenum);
  151.         hashval = bighash(tmpstr);
  152.         fp->next = fwordtotals[hashval];
  153.         fwordtotals[hashval] = fp;
  154. }
  155.  
  156. /* Looks up a file number in the hash table and
  157. ** returns the total number of words indexed in it.
  158. */
  159.  
  160. int gettotalwords(filenum)
  161.      int filenum;
  162. {
  163.         unsigned hashval;
  164.         struct fwordtotal *fp;
  165.         char tmpstr[MAXSTRLEN];
  166.  
  167.         sprintf(tmpstr, "%d", filenum);
  168.         hashval = bighash(tmpstr);
  169.         fp = fwordtotals[hashval];
  170.  
  171.         while (fp != NULL) {
  172.                 if (fp->filenum == filenum)
  173.                         return fp->totalwords;
  174.                 fp = fp->next;
  175.         }
  176.         return 0;
  177. }
  178.  
  179. /* Adds a file number to a hash table of results.
  180. ** If the entry's alrady there, add the ranks,
  181. ** else make a new entry.
  182. */
  183.  
  184. void mergeresulthashlist(filenum, rank, structure)
  185.      int filenum;
  186.      int rank;
  187.      int structure;
  188. {
  189.         unsigned hashval;
  190.         struct result *rp, *newrp;
  191.         char word[MAXWORDLEN];
  192.  
  193.         sprintf(word, "%d", filenum);
  194.     hashval = hash(word);
  195.  
  196.     rp = resulthashlist[hashval];
  197.     while (rp != NULL) {
  198.         if (rp->filenum == filenum) {
  199.             rp->rank += rank;
  200.             rp->structure |= structure;
  201.             return;
  202.         }
  203.         rp = rp->next;
  204.     }
  205.     
  206.         newrp = (struct result *) emalloc(sizeof(struct result));
  207.         newrp->filenum = filenum;
  208.         newrp->rank = rank;
  209.     newrp->structure = structure;
  210.         newrp->next = resulthashlist[hashval];
  211.         resulthashlist[hashval] = newrp;
  212. }
  213.  
  214. /* Initializes the result hash list.
  215. */
  216.  
  217. void initresulthashlist()
  218. {
  219.     int i;
  220.  
  221.     for (i = 0; i < HASHSIZE; i++)
  222.         resulthashlist[i] = NULL;
  223. }
  224.